//
//  QDLifeTableViewController.m
//  QDMetroClient
//
//  Created by YueWen on 2019/3/8.
//  Copyright © 2019 YueWen. All rights reserved.
//

#import "QDLifeTableViewController.h"
#import "QDLifeTableHeaderView.h"
#import "QDLineTextTableViewCell.h"
#import "QDMetroClient-Swift.h"
#import "QDTextListModel.h"
#import "QDWebViewController.h"
#import "QDNewsSectionHeaderView.h"
#import "UITableViewCell+QDTextListModelUpdater.h"


@interface QDLifeSectionHeaderView : QDNewsSectionHeaderView

@end


@interface QDLifeTableViewController ()<QDLifeTableHeaderViewAction>

/// 头部视图
@property (nonatomic, strong) QDLifeTableHeaderView *headerView;

/// 所有的数据

/// 轮播图
@property (nonatomic, copy) NSArray <QDCarouselItem *> *imgList;
/// 子模块
@property (nonatomic, copy) NSArray <QDCarouselItem *> *functionList;
/// 广告
@property (nonatomic, copy) NSArray <QDCarouselItem *> *adList;
/// 文章列表
@property (nonatomic, copy) NSArray <QDTextListModel *> *textLists;

@end

@implementation QDLifeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [QDUMEventManager eventObjcWithEvent:@"401"];
    self.navigationItem.title = @"生活圈";
    self.tableView.rowHeight = 101;
    [self requestAll];
    
    self.headerView = QDLifeTableHeaderView.new;
    self.headerView.actor = self;
    self.headerView.ritl_size = CGSizeMake(self.ritl_width, QDLifeTableHeaderViewHeight(true,true,false));
    
    // 优惠券
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"优惠券" style:UIBarButtonItemStylePlain target:self action:@selector(pushToLifeWebController)];
}


- (void)registerTableViewCellsAndReusableView {
    [self.tableView registerClass:QDLineTextTableViewCell.class forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:QDLifeSectionHeaderView.class forHeaderFooterViewReuseIdentifier:@"zixunHeader"];
}


- (void)headerRefreshBeginHandler {
    [self requestAll];
}


/// 请求所有信息
- (void)requestAll {
    
    ///
    [self requestLifeModuleWithComplete:^(NSDictionary<NSString *,id> * _Nonnull data) {
        
        // header信息
        self.imgList = [NSArray yy_modelArrayWithClass:QDCarouselItem.class json:[data valueForKey:@"imgList"]];
        self.functionList = [NSArray yy_modelArrayWithClass:QDCarouselItem.class json:[data valueForKey:@"functionList"]];
        self.adList = [NSArray yy_modelArrayWithClass:QDCarouselItem.class json:[data valueForKey:@"advertisement"]];
        
        // 资讯列表
        self.textLists = [NSArray yy_modelArrayWithClass:QDTextListModel.class json:[data valueForKey:@"recommend"]];
        
        //组装header
        [self.headerView updateWithItems:self.imgList items:self.functionList ads:self.adList complete:^(QDLifeTableHeaderView * _Nonnull view) {}];
        
        //设置盒子代理
        self.headerView.heziDelegate = self;
        
        self.tableView.tableHeaderView = self.headerView;
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
        
        //刷新列表
        [self.tableView reloadData];
    }];
    
    //$$$$$
    //请求优惠券
//    [self requestDiscount];
}


#pragma mark - UITableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.textLists.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    QDLineTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    [cell textUpdateByData:self.textLists[indexPath.row] atIndexPath:indexPath delegate:self associateObject:nil];
    
    cell.bottomLineView.hidden = (indexPath.row == self.textLists.count - 1);
    
    return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (self.textLists.count  == 0){ return UIView.new; }
    
    QDLifeSectionHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"zixunHeader"];
    
    view.topMargin = 11;
    view.titleLabel.text = @"生活资讯";
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return self.textLists.count == 0 ? CGFLOAT_MIN : 48;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    QDTextListModel *textModel = self.textLists[indexPath.row];
    
    [self pushToShareWebViewControllerWithUrl:textModel.webUrl shareTitle:textModel.title shareImageUrl:nil shareDesc:textModel.author autoTitle:false title:@"详情" id:textModel.identifier qdmetro:true];
}


#pragma mark - <QDLifeTableHeaderViewAction>

- (void)qdLifeTableHeaderView:(QDLifeTableHeaderView *)headerView itemIndex:(NSInteger)index {
    
    QDCarouselItem *item = self.functionList[index];
    [self routerToLifeFunctionWithId:item.moduleId title:item.function];

}


- (void)qdLifeTableHeaderView:(QDLifeTableHeaderView *)headerView bannerIndex:(NSInteger)index {
    
    [QDUMEventManager eventObjcWithEvent:@"400"];

    //
    QDCarouselItem *item = self.imgList[index];

    [self pushToShareWebViewControllerWithUrl:item.url shareTitle:item.title shareImageUrl:item.imgUrl shareDesc:nil autoTitle:false title:@"详情" id:nil qdmetro:true];
//    [self pushToWebViewControllerWithUrl:item.url  autoTitle:false title:@"详情" id:nil qdmetro:true];
}


- (void)qdLifeTableHeaderView:(QDLifeTableHeaderView *)headerView adIndex:(NSInteger)index {
    
    QDCarouselItem *item = self.adList[index];
    
    [self pushToShareWebViewControllerWithUrl:item.url shareTitle:item.title shareImageUrl:item.imgUrl shareDesc:nil autoTitle:false title:@"详情" id:nil qdmetro:true];
}

@end


@implementation QDLifeSectionHeaderView

- (CGFloat)topMargin {
    return 11;
}

@end
